package com.my.service.imp;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.my.dao.BusinessDao;
import com.my.pojo.Business;
import com.my.sender.ProductSender;
import com.my.service.IBusinessService;
import com.my.util.FaceUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;


@Service
public class BusinessServiceImpl implements IBusinessService {

    private BusinessDao businessDao;

    @Autowired
    public void setBusinessDao(BusinessDao businessDao) {
        this.businessDao = businessDao;
    }


    private RedisTemplate<String, String> redisTemplate;

    @Autowired
    public void setRedisTemplate(RedisTemplate<String, String> redisTemplate) {
        this.redisTemplate = redisTemplate;
    }

    private ProductSender productSender;

    @Autowired
    public void setProductSender(ProductSender productSender) {
        this.productSender = productSender;
    }

    @Override
    public Business loginByPassword(String userName, String password, String columnName) {
        return businessDao.findOne(
                (root, cq, cb) -> cb.and(cb.equal(root.get(columnName), userName), cb.equal(root.get("password"), password))
        );
    }

    @Override
    public Business loginByFace(String MD5) {
        String json = FaceUtil.faceSearch(MD5);
        JSONObject result = JSONObject.parseObject(json);
        if ("SUCCESS".equals(result.get("error_msg"))) {
            result = (JSONObject) result.get("result");
            JSONObject userList = ((JSONObject) ((JSONArray) result.get("user_list")).get(0));
            String score = userList.getString("score");
            String username = userList.getString("user_info");
            if (Double.parseDouble(score) > 60) {
                return this.businessDao.findOne((root, query, cb) -> cb.equal(root.get("username"), username));
            } else {
                return null;
            }
        }
        return null;
    }

    @Override
    public Business loginByPhone(String phone, String code) {
        String ori_code = redisTemplate.opsForValue().get("business" + phone);
        if (code.equals(ori_code)) {
            this.redisTemplate.delete(phone);
            return this.businessDao.findOne(((root, cq, cb) -> cb.equal(root.get("phone"), phone)));
        }
        return null;
    }

    @Override
    public Business loginByEmail(String email, String code) {
        String ori_code = redisTemplate.opsForValue().get(email);
        if (code.equals(ori_code)) {
            this.redisTemplate.delete(email);
            return this.businessDao.findOne(((root, cq, cb) -> cb.equal(root.get("email"), email)));
        }
        return null;
    }

    @Override
    public Integer sendPhoneMessage(String phone) {
        String code = this.redisTemplate.opsForValue().get(phone);
        if (code != null || businessDao.findOne((root, cq, cb) -> cb.equal(root.get("phone"), phone)) == null) {
            System.out.println(-1);
            return -1;
        }
        productSender.sendPhone(phone);
        return 0;
    }

    @Override
    public Integer sendEmailMessage(String email) {
        String code = this.redisTemplate.opsForValue().get("business" + email);
        if (code != null || businessDao.findOne((root, cq, cb) -> cb.equal(root.get("email"), email)) == null) {
            return -1;
        }
        productSender.sendEmail(email);
        return 0;
    }

    @Override
    public Integer register(Business business) {
        if (businessDao.findOne(
                (root, cq, cb) -> cb.or(cb.equal(root.get("username"), business.getUsername()),
                        cb.equal(root.get("shopName"), business.getShopName()),
                        cb.equal(root.get("phone"), business.getPhone()),
                        cb.equal(root.get("email"), business.getEmail()))) != null)
            return -1;
        JSONObject result = JSONObject.parseObject(FaceUtil.add(business.getFace(), business.getUsername()));
        String reason = result.get("error_msg").toString();
        business.setFace(((JSONObject) result.get("result")).get("face_token").toString());
        if (!reason.equals("SUCCESS")) {
            return -2;
        }
        Business res = businessDao.save(business);
        return res == null ? 0 : res.getBusinessId();
    }

    @Override
    public Business findById(Integer id) {
        return this.businessDao.findOne(id);
    }


}
